import { Component, OnInit,Input,EventEmitter,Output, ViewChild } from '@angular/core';
import {Service} from '../../server/server';
import {HttpClient} from '@angular/common/http';
import { InitService } from 'app/public/serve/init/init.service';
import { CudTableService } from 'app/public/serve/cud-table/cud-table.service';
import { HintsService } from 'app/public/serve/hints/hints.service';
import { internal } from '../internal.data';
import { idLocale } from 'ngx-bootstrap';
declare var $:any;

@Component({
    selector: 'app-internal-modal-date',
    templateUrl: './internal-modal-date.component.html',
    styleUrls: ['./internal-modal-date.component.css']
})
export class InternalModalDateComponent implements OnInit {

    @Input() internalParam : any;
    @ViewChild('addForm') formElement: HTMLFormElement;
    public internalRightClickMenuEmit: any;
    currentSelectedTxt :string = '';
    internalModalField:string = '';

    constructor( 
        private server: Service,
        public initService: InitService,
        private request: CudTableService,
        private hintsService: HintsService,
    ) { }

    ngOnInit() {
        //internalThemeWordsLeftEmit
        this.internalRightClickMenuEmit = this.server.internalRightClickMenuEmit.subscribe((value: any) => {
            if (value) {
                this.currentSelectedTxt = value;
                $('#mat-input-1').val('');
            }
        });
    }

    ngOnDestroy() {
        this.internalRightClickMenuEmit ? this.internalRightClickMenuEmit.unsubscribe() : '';
    }

    /**
   * 新增提交
   * @param info
   */
  _onSubmit(info:any){
    info.token = internal.retrieval.token;
    info.productId = this.internalParam.productId;
    info.description = '';
    info.createBy = this.server.getuserName();
    this.request
    .postRequest(this.server.baseUrl + 'Service/IdxProduct/addProductTimeMeta',info)
    .then(res =>{
      $("#internalModalDate").modal("hide");
      if(res.result_code === "0000"){
        //需要返回时间属性的id
        let content = {
            val:res.metaId,
            txt:info.metaName
        };
        this.server.internalTimeAttributeEmit.emit(content);

        this.server.onProductTimeMetaEmit.emit();
      }
      $('.custom-menu').hide();
      this.hintsService.messages(res.message,res.result_code);
      this._initAdd();
      return;
    })
    .catch(err =>{
      $("#internalModalDate").modal("hide");
      this.hintsService.messages("error","error");
      this._initAdd();
      return;
    });
  }

  /**
    * 初始化
    */
   private _initAdd(){
    this.initService._init();
    this.formElement.reset();
    $("#add").modal("hide");
   }

}
